<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Utils\ModelOperatorTrait;
use App\Exports\EntryExitRecordExport;
use Maatwebsite\Excel\Facades\Excel;

class EntryExitRecordController extends Controller
{
    use ModelOperatorTrait;

    protected function model(){
    	return app(\App\Models\EntryExitRecord::class);
    }

    public function index(Request $request){
    	$entryExitRecord = $this->queryModel($request->all());

    	if(isset($request->start_time)){
            if(isset($request->end_time)){
                $entryExitRecord = $entryExitRecord->where('readic_datetime','>=',$request->start_time)->where('readic_datetime','<=',$request->end_time);
            }
        } 

         if(request()->has('page')){
            $pageSize = 10;
            if(request()->has('pageSize')){
                $pageSize = request()->pageSize;
            }
            $entryExitRecord = $entryExitRecord->orderBy('readic_datetime','desc')->paginate($pageSize);
        }
        else{
            $entryExitRecord = $entryExitRecord->get();
        }

    	return formatJsonResponse($entryExitRecord);
    }

    public function export(Request $request){
    	 return Excel::download(new EntryExitRecordExport($request->all()), '内部车辆汇总（出入记录）'. time() .'.xlsx');
    }
}
